import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class Message {
  const Message({this.name, this.message, this.header});

  final String? name;
  final String? message;
  final String? header;

  factory Message.fromJson(Map map) {
    return Message(
      name: map['name'],
      message: map['message'],
      header: map['header'],
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();

    getData();
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: Text('首页'),
      ),
    );
  }

  void getData() async {
    // 获取数据的方法
    final url =
        Uri.parse("http://rap2api.taobao.org/app/mock/317262/api/chat/list");
    var res = await http.get(url);
    // print('statusCode: ${res.statusCode}');
    // print('body: ${res.body}');

    var obj = {
      'name': '张三',
      'message': '按们线做周最利况风历如律必术节角何。被音决他元小精任其展都认用交些。',
    };

    // 打印结果
    // print(json.encode(obj));
    // print(obj is Map);

    Message message = Message.fromJson(obj);
    print(message.name);
    print(message.message);
    print(message);
  }
}
